package com.bdc.whatwhat.task;

import android.os.AsyncTask;

import com.bdc.whatwhat.Service.Service;
import com.bdc.whatwhat.modele.WhatWhat;

import java.util.List;

/**
 * Created by florentchampigny on 25/06/14.
 */
public class DemandeWhatWhatTask extends AsyncTask<String, Void, Void> {

    public interface DemandeWhatWhatTaskTaskDelegate {
        public void afficherChargement(boolean afficher);
        public void onDemandeWhatWhatEnvoye();
        public void onErreurReseau();
    }

    DemandeWhatWhatTaskTaskDelegate delegate;
    boolean erreurReseau;
    boolean resultatVide;

    public DemandeWhatWhatTask(DemandeWhatWhatTaskTaskDelegate delegate) {
        this.delegate = delegate;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (delegate != null)
            delegate.afficherChargement(true);
    }

    @Override
    protected Void doInBackground(String... objects) {
        String utilisateur = objects[1];
        String token = objects[0];

        try {
            Service.demandeWhatWhat(utilisateur,token);
        } catch (Service.NetworkException e) {
            erreurReseau = true;
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void v) {
        super.onPostExecute(v);
        if (delegate != null) {
            delegate.afficherChargement(false);
            if (erreurReseau)
                delegate.onErreurReseau();
            else
                delegate.onDemandeWhatWhatEnvoye();
        }
    }
}
